//
//  generatexml.cxx
//  Schedule
//
//  Created by Julius Canute on 2/15/12.
//  Copyright 2012 ABB. All rights reserved.
//


#include <iostream>
#include <string>
#include <sstream>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/XMLFormatter.hpp>
#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include "XMLGenerator.h"

using namespace xercesc;
using namespace std;

int main(){
    
	    
    DOMElement *node,*root_node_main;
    XMLGenerator *xml_main = new XMLGenerator();
    {
	    
	

	
        root_node_main = xml_main->getRoot();
	int time_units=0,no_of_nodes=0, node_id = 0,cycle_time = 0,oshw_time=0,context_sw_time=0,management_time=0;
        
        cout<<"Enter The Number Of Nodes:"<<endl;
        cin>>no_of_nodes;
        {
            stringstream ss;
            ss<<no_of_nodes;
            root_node_main = xml_main->addElement(root_node_main, "Nodes", NULL, true,"no",ss.str().c_str());
        }
	
        cout<<"Enter Timing Unit(0-Micro Seconds,1-Milli Seconds,2-Seconds):"<<endl;
        cin>>time_units;
        {
            stringstream ss;
            ss<<time_units;
            xml_main->addElement(root_node_main, NULL, NULL, false,"time",ss.str().c_str());
        }
	
	

        for (int i=0; i<no_of_nodes; i++) {
            
	    XMLGenerator *xml = new XMLGenerator();
	    DOMElement *node,*root_node;
	    root_node = xml->getRoot();
	    node = xml->addElement(root_node, "Host", NULL, true);
            cout<<"Enter details for Node ("<<(i+1)<<")"<<endl;
            //I doubt this will be required
            stringstream xml_name;
	    xml_name<<"Host"<<(i+1)<<".xml";
	    xml_main->addElement(root_node_main,"NodeInfo",xml_name.str().c_str(),true);
            cout<<"Node-ID(Unique Number Representing the Node):";
            cin>>node_id;
            {
                stringstream ss;
                ss<<node_id;
                xml->addElement(node, "NodeId",ss.str().c_str(), true);
            }
            cout<<"Cycle-Time(Integer):";
            cin>>cycle_time;
            {
                stringstream ss;
                ss<<cycle_time;
                xml->addElement(node, "CycleTime",ss.str().c_str(), true);
            }
            cout<<"OS/HW-Time(Integer):";
            cin>>oshw_time;
            {
                stringstream ss;
                ss<<oshw_time;
                xml->addElement(node, "OSHWTime",ss.str().c_str(), true);
            }
            cout<<"Context-Switch-Time(Integer):";
            cin>>context_sw_time;
            {
                stringstream ss;
                ss<<context_sw_time;
                xml->addElement(node, "ContextSWTime",ss.str().c_str(), true);
            }
            cout<<"Management-Time(Integer):";
            cin>>management_time;
            {
                stringstream ss;
                ss<<management_time;
                xml->addElement(node, "ManagementTime",ss.str().c_str(), true);
            }
	
        xml->SerializeToFile(xml_name.str().c_str());
	delete xml;
        }

    }
    
    {

        DOMElement *tasks,*task,*groups,*group,*d_dependants,*depend,*jobs,*job;

        int no_of_tasks = 0,no_of_jobs = 0,cycle_time=0,deadline=0,dependants=0,dependant_no=0,job_duration=0,job_constraint=0,inseperable_group=0,no_of_jobs_group=0,group_job_no=0;
        cout<<"Enter The Number of Applications:"<<endl;
        cin>>no_of_tasks;
        {
            stringstream ss;
            ss<<no_of_tasks;
            tasks = xml_main->addElement(root_node_main, "Applications", NULL, true,"no",ss.str().c_str());
        }

        for (int i=0; i<no_of_tasks; i++) {

	    stringstream app_name;
	    XMLGenerator *xml = new XMLGenerator();
	    tasks = xml->getRoot();
	    

            cout<<"Enter The Details for Application("<<(i+1)<<")"<<endl;
	    app_name<<"Application"<<(i+1)<<".xml";
	    xml_main->addElement(root_node_main,"ApplicationInfo",app_name.str().c_str(),true);


            cout<<"Number of Components:"<<endl;
            cin>>no_of_jobs;
            {
                stringstream ss;
                ss<<no_of_jobs;
                task = xml->addElement(tasks, "Application",NULL, true,"jobno",ss.str().c_str());
            }
            cout<<"Cycle Time(Integer):"<<endl;
            cin>>cycle_time;
            {
                stringstream ss;
                ss<<cycle_time;
                xml->addElement(task, NULL,NULL, false,"cycle",ss.str().c_str());
            }
            cout<<"Deadline(Integer):"<<endl;
            cin>>deadline;
            {
                stringstream ss;
                ss<<deadline;
                xml->addElement(task, NULL,NULL, false,"deadline",ss.str().c_str());
            } 
            cout<<"Number of Inseparable Group:"<<endl; 
            cin>>inseperable_group;
            {
                stringstream ss;
                ss<<inseperable_group;
                groups =  xml->addElement(task, "Groups",NULL, true,"no",ss.str().c_str());
            }
            for (int l=0; l<inseperable_group; l++) {
                cout<<"Details of Group("<<(l+1)<<"):"<<endl;
                cout<<"Number of Components:"<<endl;
                
                cin>>no_of_jobs_group;
                {
                    stringstream ss;
                    ss<<no_of_jobs_group;
                    group = xml->addElement(groups, "Group",NULL, true,"no",ss.str().c_str());
                }
                 
                
                cout<<"Enter Job Number for This Group(1..."<<(no_of_jobs_group)<<")"<<endl;
                for (int m=0; m<no_of_jobs_group; m++) {
                    cin>>group_job_no;
                    {
                        stringstream ss;
                        ss<<group_job_no;
                        xml->addElement(group, "Item", ss.str().c_str(), true);
                    }
                }
            }
            
            jobs = xml->addElement(task, "Components", NULL, true);
            
            for (int j=0; j<no_of_jobs; j++) {
                job = xml->addElement(jobs, "Component", NULL, true);
                cout<<"Enter Details of Component("<<(j+1)<<") for Application("<<(i+1)<<")"<<endl;
                cout<<"Job Duration(Integer):"<<endl;
                cin>>job_duration;
                {
                    stringstream ss;
                    ss<<job_duration;
                    xml->addElement(job, "JobDuration", ss.str().c_str(), true);
                }
                cout<<"Start Constraint(Integer(0-[If No Constraint])):";
                cin>>job_constraint;
                {
                    stringstream ss;
                    ss<<job_constraint;
                    xml->addElement(job, "JobConstraint", ss.str().c_str(), true);
                }
	    }

            xml->SerializeToFile(app_name.str().c_str());
	    delete xml;
        }
    }
    string name;
    
    cout<<"Enter The Name of XML File:"<<endl;
    cin>>name;
    xml_main->SerializeToFile(name.c_str());
    delete xml_main;
   
    return 0;
}
